using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using MyPhotoIndex.ImageBank;
using System.Globalization;
using MyPhotoIndex.Utilities;
using MyPhotoIndex.DataAccess;
using System.IO;

namespace MyPhotoIndex.Settings
{
    public partial class CacheSettingsForm : FormTemplate
    {
        public CacheSettingsForm()
        {
            InitializeComponent();

            thumbnailSizeComboBox.Items.Add(new Size(240, 160));
            thumbnailSizeComboBox.Items.Add(new Size(320, 240));
            thumbnailSizeComboBox.Items.Add(new Size(640, 480));

            thumbnailSizeComboBox.SelectedItem = ApplicationSettings.Default.CachedThumbnailSize;
            moveCacheFileButton.Click += new EventHandler(moveCacheFileButton_Click);

            shrinkButton.Click += new EventHandler(shrinkButton_Click);

            if (ApplicationFolders.IsAppPortable == true)
            {
                moveCacheFileButton.Enabled = false;
            }

            GetCacheInfo();
        }

        void moveCacheFileButton_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = ApplicationFolders.CachePath;
            DialogResult dr = folderBrowserDialog1.ShowDialog(this);
            if (dr == DialogResult.Cancel)
            {
                return;
            }

            string newPath = folderBrowserDialog1.SelectedPath;
            if (string.Compare(newPath, ApplicationFolders.CachePath, true, CultureInfo.InvariantCulture) == 0)
            {
                return;
            }

            DialogResult dialogResult = ApplicationAlert.Show(Resources.Resources.MoveCahceMessage + " " + newPath, Resources.Resources.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult != DialogResult.Yes)
            {
                return;
            }

            thumbnailSizeComboBox.Enabled = false;
            moveCacheFileButton.Enabled = false;

            base.CancelButtonEnabled = false;
            base.OkButtonEnabled = false;

            try
            {
                //Get the current index id
                string indexId = DataAccessProvider.Instance.GetDbIndexId();
                indexId = Path.ChangeExtension(indexId, "icf");

                //Disable image cache
                Storage.Instance.UseImageCache(false);

                //Copy the current index file
                Microsoft.VisualBasic.FileIO.FileSystem.MoveFile(Path.Combine(ApplicationFolders.CachePath, indexId), Path.Combine(newPath, indexId), 
                            Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, 
                            Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException);
               
                ApplicationFolders.CachePath = newPath;
                ApplicationSettings.Default.ImageCachePath = newPath;
                ApplicationSettings.Default.Save();
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
            finally
            {
                Storage.Instance.UseImageCache(true);
            }

            thumbnailSizeComboBox.Enabled = true;
            moveCacheFileButton.Enabled = true;

            base.CancelButtonEnabled = true;
            base.OkButtonEnabled = true;
            GetCacheInfo();
        }

        private void GetCacheInfo()
        {
            long totalSizeKb = (Storage.Instance.GetCachePhysicalSizeBytes() / 1024);
            long unusedSizeKb = (Storage.Instance.GetCacheFreeSpaceBytes() / 1024);
            this.totalSizeLabel.Text = totalSizeKb.ToString(CultureInfo.InvariantCulture) + "Kb";
            this.unusedSpaceLabel.Text = unusedSizeKb.ToString(CultureInfo.InvariantCulture) + "Kb";

            if (unusedSizeKb < 5000)
            {
                shrinkButton.Enabled = false;
            }
            else
            {
                shrinkButton.Enabled = true;
            }

            cacheFilePathTextBox.Text = ApplicationFolders.CachePath;
        }

        void shrinkButton_Click(object sender, EventArgs e)
        {
            shrinkButton.Enabled = false;
            colorProgressBar1.Visible = true;
            unusedSpaceLabel.Visible = false;
            label6.Visible = false;
            thumbnailSizeComboBox.Enabled = false;
            moveCacheFileButton.Enabled = false;

            base.CancelButtonEnabled = false;
            base.OkButtonEnabled = false;

            Storage.Instance.ImageCollectionProgressEvent += new EventHandler<ImageCollectionProgressEventArgs>(Instance_ImageCollectionProgressEvent);
            Storage.Instance.ReindexCache();
            colorProgressBar1.Visible = false;
            unusedSpaceLabel.Visible = true;
            label6.Visible = true;
            thumbnailSizeComboBox.Enabled = true;
            moveCacheFileButton.Enabled = true;

            base.CancelButtonEnabled = true;
            base.OkButtonEnabled = true;

            GetCacheInfo();
        }

        void Instance_ImageCollectionProgressEvent(object sender, ImageCollectionProgressEventArgs e)
        {
            if (colorProgressBar1.Maximum != e.Maximum)
            {
                colorProgressBar1.Maximum = e.Maximum;
            }

            if (e.Value % 300 == 0)
            {
                colorProgressBar1.Value = e.Value;
                Application.DoEvents();
            }
        }

        protected override void OkButton_Click(object sender, EventArgs e)
        {
            ApplicationSettings.Default.CachedThumbnailSize = (Size)thumbnailSizeComboBox.SelectedItem;
            base.OkButton_Click(sender, e);
        }
    }
}